﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;

//场景管理
public class SceneMgr : Singleton<SceneMgr>
{
    public   static void OnEnterMap(Cmd cmd)
    {
        if (!Net.CheckCmd(cmd, typeof(EnterMapCmd))) { return; };
        EnterMapCmd enterMapCmd = cmd as EnterMapCmd;

        SceneMgr.instance.LoadScene(enterMapCmd.MapID);
    }

    //加载场景
    private  void LoadScene(int mapID)
    {
        var mapDatabase = MapTable.instance[mapID];
        if (mapDatabase==null)
        {
            Debug.LogError("未找到地图：" + mapID);
            return;
        }

        //阻塞消息，暂时不处理消息
        Net.instance.Pause = true;
       var ao= SceneManager.LoadSceneAsync(mapDatabase.ScenePath);
        QuickCoroutine.instance.StartCoroutine(loadEnd(ao));
        //SceneManager.LoadScene(mapDatabase.ScenePath);

    }

    private IEnumerator loadEnd(AsyncOperation ao)
    {
        while (!ao.isDone)
        {
            //加载进度 
            yield return new WaitForEndOfFrame();
        }
        Debug.Log("场景加载完毕");
        //放开消息
        Net.instance.Pause = false;

    }
}